package com.wm.dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.Order;
import com.wm.bean.OrderRes;
import com.wm.dao.OrderResDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class OrderResHibernateDAO extends HibernateDaoSupport implements OrderResDAO {

	private static final String LOAD_BY_ID = "from OrderRes where id = ?";
	private static final String LOAD_BY_ORDERID = "from OrderRes where orderId = ?";
	private static final String LOAD_BY_USERID = "from OrderRes where userId = ?";
	private static final String LOAD_BY_RESID = "from OrderRes where resId = ?";
	
	/**
	 * 保存
	 * @param o
	 * @return
	 */
	public OrderRes saveOrderRes(OrderRes o){
		this.getHibernateTemplate().save(o);
		return o;
	}
	
	/**
	 * 修改
	 * @param o
	 * @return
	 */
	public OrderRes updateOrderRes(OrderRes o){
		this.getHibernateTemplate().update(o);
		return o;
	}
	
	/**
	 * 删除
	 * @param o
	 */
	public void removeOrderRes(OrderRes o){
		this.getHibernateTemplate().delete(o);
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	public OrderRes findOrderResById(long id){
		Object[] obj={new Long(id)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (OrderRes) l.get(0);
		}
	}
	
	/**
	 * 根据订单ID查询
	 * @param orderId
	 * @return
	 */
	public OrderRes findOrderResByOrderId(long orderId){
		Object[] obj={new Long(orderId)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ORDERID,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (OrderRes) l.get(0);
		}
	}
	
	/**
	 * 根据用户ID查询
	 * @param userId
	 * @return
	 */
	public List findOrderResByUserId(long userId){
		return this.getHibernateTemplate().find(LOAD_BY_USERID,new Long(userId));
	}
	
	/**
	 * 根据饭店ID查询
	 * @param resId
	 * @return
	 */
	public List findOrderResByResId(long resId){
		return this.getHibernateTemplate().find(LOAD_BY_RESID,new Long(resId));
	}

}
